package com.example.model.dao;

import com.example.model.com.entity.Users;
import com.example.model.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    //用户注册
    public int add(Users user) {
        //创建连接对象
        Connection connection = null;
        //创建预处理对象
        PreparedStatement pst = null;

        String sql = "insert into users(userName,password,sex,email)" +
                "values(?,?,?,?)";
        connection = JDBCUtil.getConnection();
        int result = 0;
        try {
            pst = connection.prepareStatement(sql);
            pst.setString(1, user.getUserName());
            pst.setString(2, user.getPassword());
            pst.setString(3, user.getSex());
            pst.setString(4, user.getEmail());
            result = pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    //查询所有用户信息
//	public List finAll() {
//		String sql="select * from users";
//		PreparedStatement ps = util.createStatement(sql);
//		ResultSet rs=null;
//	    List userList =new ArrayList();
//	    try {
//			rs = ps.executeQuery();
//			//获取数据
//			while(rs.next()){
//				Integer userId = rs.getInt("userId");
//				String userName = rs.getString("username");
//				String password = rs.getString("password");
//				String sex = rs.getString("sex");
//				String email=rs.getString("email");
//				//获取结果放入Users类里
//				Users users =new Users(userId,userName,password,sex,email);
//				//获取的users类数据存入集合中
//				userList.add(users);
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}finally {
//			util.close(rs);
//		}
//	    return 	userList;
//	}

//	//删除用户信息根据用户编号
//	public int delete(String userId){
//		//预编译sql语句
//		String sql="delete from users where userId=?";
//		PreparedStatement ps = util.createStatement(sql);
//		int result=0;
//		//收数据
//		try {
//			ps.setInt(1, Integer.valueOf(userId));
//			result=ps.executeUpdate();
//		} catch (NumberFormatException e) {
//			e.printStackTrace();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}finally {
//
//			util.close();
//		}
//		return result;
//	}

    //登录验证
    public int login(String userName, String password) {
        //创建连接对象
        Connection connection = null;
        //创建预处理对象
        PreparedStatement pst = null;

        //预编译sql语句
        String sql = "select count(*) from users where username=? and password=?";
        connection = JDBCUtil.getConnection();
        ResultSet rs = null;
        int result = 0;
        try {
            pst = connection.prepareStatement(sql);
            pst.setString(1, userName);
            pst.setString(2, password);
            rs = pst.executeQuery();
            while (rs.next()) {
                result = rs.getInt("count(*)");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeAll(rs, pst, connection);
        }
        return result;
    }

}
